# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=discount
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.6
pkgrel=1
pkgdesc="A Markdown implementation written in C (mingw-w64)"
arch=('any')
url="https://www.pell.portland.or.us/~orc/Code/discount/"
license=('custom:BSD')
provides=("${MINGW_PACKAGE_PREFIX}-markdown")
conflicts=("${MINGW_PACKAGE_PREFIX}-markdown")
options=('strip' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/Orc/discount/archive/v${pkgver}.tar.gz"
        "discount-mingw-building.patch")
sha256sums=('0beb98a66a6e38ed125e2d0a161ff6b461f6a38538bb72498766f376c94a0ad3'
            '1dafea93f8d546f00b3f57928124392e28fd30e4ef60268736d4837075e3e462')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/discount-mingw-building.patch
  sed -e "s/m 444/m 644/g" -i configure.inc
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  cp -rf "${srcdir}"/${_realname}-${pkgver} "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  CC=${MINGW_PREFIX}/bin/gcc.exe \
  AC_CPP_PROG=${MINGW_PREFIX}/bin/cpp.exe \
  AC_LN_PROG="cp -pR" \
  ./configure.sh \
      --prefix=${MINGW_PREFIX} \
      --enable-all-features \
      --shared

  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,include,lib}
  make PREFIX=${MINGW_PREFIX} DESTDIR="${pkgdir}" install.everything
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin/
  install -Dm644 COPYRIGHT "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  find ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig -name *.pc -exec sed -i -e"s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" {} \;
}
